package com.tianzehua.netty.heartbeatexample;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * heartbeat server initializer
 * @author tianzehua
 * @date 2019/03/10
 */
public class MyServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline channelPipeline = ch.pipeline();
        /* netty 提供的空闲检测机制，当一定的时间段内没有进行读，写，读或者写的话，就会触发该handler */
        channelPipeline.addLast(new IdleStateHandler(5,7,10, TimeUnit.SECONDS));
        channelPipeline.addLast(new MyServerHandler());
    }
}
